﻿using System;
using System.Configuration;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using mojoPortal.Web.RS.SBM.AppCode.Lib;

namespace mojoPortal.Web.RS.SBM.Controls
{
    public partial class WorkOrderPartListControl : RSBaseUserControl
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            

        }

        public void LoadWorkOrderPartListByID(int companyid, int WorkOrderid)
        {
            SqlCommand myCommand;
            SqlParameter param;
            SqlConnection myConnection;

            String sqlSELECT = "select pp.WorkOrderproductid, pp.WorkOrderid, pp.productid, pp.quantity, pp.installdate, i.productname, i.unitsinstock, i.ModelNo from rs_tblWorkOrderPart as pp left join rs_tblInventory i on pp.ProductId=i.productid where pp.workorderid=@WorkOrderid and pp.companyid=@companyid order by productname";

            myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
            myCommand = new SqlCommand(sqlSELECT, myConnection);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@companyid";
            param.Value = companyid;
            myCommand.Parameters.Add(param);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@WorkOrderid";
            param.Value = WorkOrderid;
            myCommand.Parameters.Add(param);

            SqlDataAdapter adapter = new SqlDataAdapter(myCommand);
            myConnection.Open();
            DataSet ds = new DataSet();
            adapter.Fill(ds);

            gvWorkOrderPartList.DataSource = ds;
            gvWorkOrderPartList.DataBind();
            myConnection.Close();

        }

        //protected void gvWorkOrderPartList_RowDeleting(Object sender, GridViewDeleteEventArgs e)
        //{
        //    RSUserIdentity userIdentity = GetRSUserIdentity();
        //    if (userIdentity.RSCanWrite && userIdentity.RSCanDelete)
        //    {
        //        int companyid = userIdentity.RSCompanyId;
        //        int WorkOrderproductid = Convert.ToInt32(gvWorkOrderPartList.DataKeys[e.RowIndex].Value);
        //        RemoveWorkOrder(companyid, WorkOrderproductid);
        //    }
        //    else
        //        ucModalActionMessageControl.ShowModal("You Do Not Have Permission To Perform This Action!", "", false);
        //}

        protected void gvProduct_Edit(Object sender, CommandEventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            if (userIdentity.RSCanWrite)
            {
                int companyid = userIdentity.RSCompanyId;
                int productid = Convert.ToInt32(e.CommandArgument.ToString());
                ucModalProductControl.ShowModal(companyid, productid, false);
            }
            else
                ucModalActionMessageControl.ShowModal("You Do Not Have Permission To Perform This Action!", "", false);
        }
        protected void gvProduct_Remove(Object sender, CommandEventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            if (userIdentity.RSCanWrite && userIdentity.RSCanDelete)
            {
                int companyid = userIdentity.RSCompanyId;
                int WorkOrderproductid = Convert.ToInt32(e.CommandArgument.ToString());
                if (companyid > 0 && WorkOrderproductid > 0)
                {
                    SqlCommand sqlCmd = new SqlCommand();
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@WorkOrderproductid", SqlDbType.Int, 0, ParameterDirection.Input, WorkOrderproductid);
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@companyid", SqlDbType.Int, 0, ParameterDirection.Input, companyid);
                    SQLHelper.SetCommandType(sqlCmd, CommandType.Text, "DELETE FROM rs_tblWorkOrderPart WHERE WorkOrderproductid=@WorkOrderproductid AND companyid=@companyid");
                    SQLHelper.ExecuteScalarCmd(sqlCmd);
                    int returnValue = Convert.ToInt32(sqlCmd.Parameters["@ReturnValue"].Value);
                    ucModalActionMessageControl.ShowModal("Product removed successfully!", Request.RawUrl, true);
                }
            }
            else
                ucModalActionMessageControl.ShowModal("You Do Not Have Permission To Perform This Action!", "", false);
        }

    }

    
}